package ModelLayer;

import java.io.Serializable;
import java.text.DecimalFormat;

public class SalesLine implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int quantity;
	private SaleItem saleItem;
	private double subtotalWithDiscount;
	private double subtotalWithoutDiscount;
	private double subtotalDiscount;
	private Sale sale;
	
	public SalesLine(Sale sale, int quantity, String barcode) throws RealException
	{
		this.sale = sale;
		this.quantity = quantity;
		saleItem = SaleItemRecords.getInstance().getSaleItem(barcode);
		
	}
	
	// whether the saleline get quantity discount
	public boolean getDiscountQ()
	{
		if (quantity > saleItem.getDiscountQ()) {
			return true;
		}
		else return false;
	}
	
	// total price for the saleline with tax and without discount
	public void calculateSubtotalWithoutDiscount() {
		
		double subtotal = Double.valueOf(new DecimalFormat("#.##").format(saleItem.getsPrice()* quantity * (1 + saleItem.getTax())));
		setSubtotalWithoutDiscount(subtotal);
	}
	
	public void calculateSubtotalWithDiscount() {
		calculateSubtotalDiscount();
		setSubtotalWithDiscount(getSubtotalWithoutDiscount() * (1-getSubtotalDiscount()));
	}
	
	public void calculateSubtotalDiscount()
	{
		double discount = 0;
		if (getDiscountQ()) {
			discount += sale.getCustomer().getGroup().getQuantityDiscount();
		}
		if (sale.getPickup()) {
			discount += sale.getCustomer().getGroup().getPickUpDiscount();
		}
		discount += sale.getCustomer().getGroup().getCraftmanDiscount();
		setSubtotalDiscount(discount);
	}

	public String toString()
	{
		return "Product: " + saleItem.getName() + ", Quantity: " + quantity + "\nUnit price w/o tax: "  + saleItem.getsPrice() + ", Tax: "  + Double.valueOf(new DecimalFormat("#.##").format(saleItem.getTax() * 100))  + "%\nProduct location: " + saleItem.getLocation() + "\nPrice w/o discount: " + Double.valueOf(new DecimalFormat("#.##").format(getSubtotalWithoutDiscount())) + "\nPrice w/ discount: " + Double.valueOf(new DecimalFormat("#.##").format(getSubtotalWithDiscount())) + ", Discount: " + Double.valueOf(new DecimalFormat("#.##").format(getSubtotalDiscount() * 100)) + "%\n\n";
	}
	
	public String getBarcode()
	{
		return saleItem.getBarcode();
	}

	public int getQuantity() {
		return quantity;
	}


	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}


	public SaleItem getSaleItem() {
		return saleItem;
	}


	public void setProduct(SaleItem saleItem) {
		this.saleItem = saleItem;
	}

	public void setSubtotalWithDiscount(double subtotalWithDiscount)
	{
		this.subtotalWithDiscount = subtotalWithDiscount;
	}
	
	public double getSubtotalWithDiscount()
	{
		return subtotalWithDiscount;
	}
	
	public void setSubtotalDiscount(double subtotalDiscount)
	{
		this.subtotalDiscount = subtotalDiscount;
	}
	
	public double getSubtotalDiscount()
	{
		return subtotalDiscount;
	}

	public double getSubtotalWithoutDiscount() {
		return subtotalWithoutDiscount;
	}

	public void setSubtotalWithoutDiscount(double subtotalWithoutDiscount) {
		this.subtotalWithoutDiscount = subtotalWithoutDiscount;
	}

	public void setSaleItem(SaleItem saleItem) {
		this.saleItem = saleItem;
	}
	
	
}